import api from "../../api"
import jwt from 'jwt-decode'
import router from '../../router'
import element from 'element-ui'
export default {
    namespaced: true,
    state: {
        userInfo: {
            user: "",
            token: "",
            isLogin: false,
        }
    },
    mutations: {
        setUser(state, params) {
            state.userInfo = params;
        }
    },
    actions: {
        // promise  函数().then()
        // async await
        async getLoginActions({ commit }, params) {
            let res = await api.getLogin(params)
            // console.log("登录界面", res);
            if (res.status === 200) {
                // console.log(jwt(res.data));
                let temp = {
                    user: jwt(res.data).username,
                    token: res.data,
                    isLogin: Boolean(res.data),
                };
                // 存储 token 信息  -- localstorage  vuex-state
                commit("setUser", temp);
                localStorage.setItem("userToken", JSON.stringify(temp));
                // 页面跳转
                router.push("/commodity");
            } else {
                element.Message.error("信息获取错误")
                console.log("信息获取错误");
            }
        }
    },
    getters: {}
}